package com.itic.mall.system.users.collectionitem.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.mall.system.inter.users.collectionitem.persistence.dao.UsersCollectItemDao;
import com.itic.mall.system.inter.users.collectionitem.persistence.model.UsersCollectItem;
import com.itic.mall.system.inter.users.collectionitem.persistence.model.UsersCollectItemExample;
import com.itic.mall.system.inter.users.collectionitem.persistence.model.UsersCollectItemExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UsersCollectItemService
     extends CrudService<UsersCollectItemDao,UsersCollectItem,UsersCollectItemExample> {
    @Autowired
    private UsersCollectItemDao usersCollectItemDao;
    
    public void delCollectItemByUserIdAndStoreInfo(String userId,String storeId) {
    	UsersCollectItemExample example = new UsersCollectItemExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andStoreIdEqualTo(storeId);
    	List<UsersCollectItem> usersCollectItems= this.list(example);
    	for(UsersCollectItem item:usersCollectItems)
    		this.delete(item);
    }
    
    public void delCollectItemByUserIdAndGoodsId(String userId,String goodsId) {
    	UsersCollectItemExample example = new UsersCollectItemExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andGoodsIdEqualTo(goodsId);
    	List<UsersCollectItem> usersCollectItems= this.list(example);
    	for(UsersCollectItem item:usersCollectItems)
    		this.delete(item);
    }
    
    public UsersCollectItem getCollectItemByUserIdAndGoodsId(String userId,String goodsId){
    	UsersCollectItemExample example = new UsersCollectItemExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andGoodsIdEqualTo(goodsId);
    	List<UsersCollectItem> usersCollectItems= this.list(example);
    	if(usersCollectItems.isEmpty())
    		return null;
    	else
    		return usersCollectItems.get(0);
    	
    }

}